import {ProjectItem } from "./ProjectItem.js";
import {DOMHelper} from "../Utility/DOMHelper.js";

export class ProjectList{
    projects =[];
    constructor(type) {
        this.type = type;
        const prjItems = document.querySelectorAll(`#${type}-projects li`);
        for(const prjItem of prjItems) {
            this.projects.push(new ProjectItem(prjItem.id,this.switchProject.bind(this),this.type));
        }
    }

    setSwitchHandlerFunction(switchHandlerFunction){
        this.switchHandler = switchHandlerFunction;
    }

    addProject(project){
        console.log(project);
        this.projects.push(project);
        DOMHelper.moveElement(project.id,`#${this.type}-projects ul`);
        project.update(this.switchProject.bind(this),this.type);

    }
    switchProject(projectId){
        // const projectIndex = this.projects.findIndex(p => p.id === projectId);
        // this.projects.splice(projectIndex, 1);
        this.switchHandler(this.projects.find(p => p.id === projectId));
        this.projects = this.projects.filter(p => p.id !== projectId);
    }
}